#region Imports
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

#endregion

namespace WEALTH
{
    /// <summary>
    ///DataService
    /// </summary>
    public class BaseDao
    {
        #region Connection
        internal string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        internal SqlConnection Connection;
        #endregion

        #region Constructeur
        public BaseDao()
        {
            Connection = new SqlConnection();
            Connection.ConnectionString = connectionString;
        }
        #endregion

        #region ExecuteDataSet
        /// <summary>
        /// ExecuteDataset
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="commandType"></param>
        /// <param name="commandText"></param>
        /// <param name="dataTable"></param>
        /// <param name="commandParameters"></param>
        public static void ExecuteDataset(SqlConnection connection,
            CommandType commandType, string commandText, DataTable dataTable,
            params SqlParameter[] commandParameters)
        {
            //create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();

            //if the provided connection is not open, we will open it
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            cmd.Connection = connection;
            cmd.CommandText = commandText;
            cmd.CommandType = commandType;

            //attach the command parameters if they are provided
            if (commandParameters != null)
            {
                foreach (IDataParameter p in commandParameters)
                {
                    //check for derived output value with no value assigned
                    if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
                    {
                        p.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(p);
                }
            }

            //create the DataAdapter
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            //fill the DataTable
            da.Fill(dataTable);
            cmd.Parameters.Clear();
            connection.Close();
        }
        #endregion

        #region ExecuteDataSet
        /// <summary>
        /// ExecuteDataset
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="commandType"></param>
        /// <param name="commandText"></param>
        /// <param name="dataTable"></param>
        /// <param name="commandParameters"></param>
        public static void ExecuteDataset(SqlTransaction transaction,
            CommandType commandType, string commandText, DataTable dataTable,
            params SqlParameter[] commandParameters)
        {
            //create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();
            SqlConnection connection = new SqlConnection();
            connection = transaction.Connection;

            //if the provided connection is not open, we will open it
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            cmd.Transaction = transaction;
            cmd.Connection = connection;
            cmd.CommandText = commandText;
            cmd.CommandType = commandType;

            //attach the command parameters if they are provided
            if (commandParameters != null)
            {
                foreach (IDataParameter p in commandParameters)
                {
                    //check for derived output value with no value assigned
                    if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
                    {
                        p.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(p);
                }
            }

            //create the DataAdapter
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            //fill the DataTable
            da.Fill(dataTable);
            cmd.Parameters.Clear();
        }
        #endregion
    }
}
